package com.han.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 随机数工具
 * @author Administrator
 *
 */
public class RandomNumUtil {

	/**
	 * 日期格式
	 */
	private static final String DATE_PATTERN1 = "yyyyMMddHHmmss";
	
	
	/**
	 * 根据当前日期生成随机数据
	 * @return
	 */
	public static String genRandomNum() {
		StringBuilder sb = new StringBuilder();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN1);
		String format = sdf.format(new Date());
		// 获取两位随机数
		String suff = getNumByBit(2);
		return sb.append(format).append(String.valueOf(suff)).toString();
	}
	
	/**
	 * 获取指定位数的随机数
	 * @param length
	 * @return
	 */
	public static String getNumByBit(int length) {
		StringBuilder val = new StringBuilder("");
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			val.append(String.valueOf(random.nextInt(10)));
		}
		return val.toString();
	}
	
}
